<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Callback," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="回调接口的举例和实现。">
<meta property="og:type" content="article">
<meta property="og:title" content="回调接口">
<meta property="og:url" content="http://yoursite.com/2017/02/27/201702/回调接口/index.html">
<meta property="og:site_name" content="HoneyGuide's Blog">
<meta property="og:description" content="回调接口的举例和实现。">
<meta property="og:updated_time" content="2017-02-27T04:34:58.454Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="回调接口">
<meta name="twitter:description" content="回调接口的举例和实现。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'R1W2VDDO1V',
      apiKey: '773854c4c250e5a5cf9cddd97ebcc167',
      indexName: 'Hexo_search',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/02/27/201702/回调接口/"/>





  <title> 回调接口 | HoneyGuide's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">﻿<div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">HoneyGuide's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Stay hungry & Stay young</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">

　　　　　　<!--增加的代码 start-->

          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/02/27/201702/回调接口/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HoneyGuide">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="HoneyGuide's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                回调接口
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-27T00:00:00+08:00">
                2017-02-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>回调接口的举例和实现。<br><a id="more"></a></p>
<h3 id="回调"><a href="#回调" class="headerlink" title="回调"></a>回调</h3><p>就是A类中调用B类中的某个方法C，然后B类中反过来调用A类中的方法D，D这个方法就叫回调方法。</p>
<ul>
<li>class A实现接口CallBack callback——背景1</li>
<li>class A中包含一个class B的引用b ——背景2</li>
<li>class B有一个参数为callback的方法f(CallBack callback) ——背景3</li>
<li>A的对象a调用B的方法 f(CallBack callback) ——A类调用B类的某个方法 C</li>
<li>然后b就可以在f(CallBack callback)方法中调用A的方法 ——B类调用A类的某个方法D</li>
</ul>
<h3 id="举例"><a href="#举例" class="headerlink" title="举例"></a>举例</h3><p>有一天小王遇到一个很难的问题，问题是“1 + 1 = ?”，就打电话问小李，小李一下子也不知道，就跟小王说，等我办完手上的事情，就去想想答案，小王也不会傻傻的拿着电话去等小李的答案吧，于是小王就对小李说，我还要去逛街，你知道了答案就打我电话告诉我，于是挂了电话，自己办自己的事情，过了一个小时，小李打了小王的电话，告诉他答案是2。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * 这是一个回调接口 </div><div class="line"> * @author xiaanming </div><div class="line"> * </div><div class="line"> */  </div><div class="line">public interface CallBack &#123;  </div><div class="line">    /** </div><div class="line">     * 这个是小李知道答案时要调用的函数告诉小王，也就是回调函数 </div><div class="line">     * @param result 是答案 </div><div class="line">     */  </div><div class="line">    public void solve(String result);  </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * 这个是小王 </div><div class="line"> * @author xiaanming </div><div class="line"> * 实现了一个回调接口CallBack，相当于-----&gt;背景一 </div><div class="line"> */  </div><div class="line">public class Wang implements CallBack &#123;  </div><div class="line">    /** </div><div class="line">     * 小李对象的引用 </div><div class="line">     * 相当于-----&gt;背景二 </div><div class="line">     */  </div><div class="line">    private Li li;   </div><div class="line">  </div><div class="line">    /** </div><div class="line">     * 小王的构造方法，持有小李的引用 </div><div class="line">     * @param li </div><div class="line">     */  </div><div class="line">    public Wang(Li li)&#123;  </div><div class="line">        this.li = li;  </div><div class="line">    &#125;  </div><div class="line">      </div><div class="line">    /** </div><div class="line">     * 小王通过这个方法去问小李的问题 </div><div class="line">     * @param question  就是小王要问的问题,1 + 1 = ? </div><div class="line">     */  </div><div class="line">    public void askQuestion(final String question)&#123;  </div><div class="line">        //这里用一个线程就是异步，  </div><div class="line">        new Thread(new Runnable() &#123;  </div><div class="line">            @Override  </div><div class="line">            public void run() &#123;  </div><div class="line">                /** </div><div class="line">                 * 小王调用小李中的方法，在这里注册回调接口 </div><div class="line">                 * 这就相当于A类调用B的方法C </div><div class="line">                 */  </div><div class="line">                li.executeMessage(Wang.this, question);   </div><div class="line">            &#125;  </div><div class="line">        &#125;).start();  </div><div class="line">          </div><div class="line">        //小网问完问题挂掉电话就去干其他的事情了，诳街去了  </div><div class="line">        play();  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    public void play()&#123;  </div><div class="line">        System.out.println(&quot;我要逛街去了&quot;);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    /** </div><div class="line">     * 小李知道答案后调用此方法告诉小王，就是所谓的小王的回调方法 </div><div class="line">     */  </div><div class="line">    @Override  </div><div class="line">    public void solve(String result) &#123;  </div><div class="line">        System.out.println(&quot;小李告诉小王的答案是---&gt;&quot; + result);  </div><div class="line">    &#125;  </div><div class="line">      </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * 这个就是小李啦 </div><div class="line"> * @author xiaanming </div><div class="line"> * </div><div class="line"> */  </div><div class="line">public class Li &#123;  </div><div class="line">    /** </div><div class="line">     * 相当于B类有参数为CallBack callBack的f()----&gt;背景三 </div><div class="line">     * @param callBack   </div><div class="line">     * @param question  小王问的问题 </div><div class="line">     */  </div><div class="line">    public void executeMessage(CallBack callBack, String question)&#123;  </div><div class="line">        System.out.println(&quot;小王问的问题---&gt;&quot; + question);  </div><div class="line">          </div><div class="line">        //模拟小李办自己的事情需要很长时间  </div><div class="line">        for(int i=0; i&lt;10000;i++)&#123;  </div><div class="line">              </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        /** </div><div class="line">         * 小李办完自己的事情之后想到了答案是2 </div><div class="line">         */  </div><div class="line">        String result = &quot;答案是2&quot;;  </div><div class="line">          </div><div class="line">        /** </div><div class="line">         * 于是就打电话告诉小王，调用小王中的方法 </div><div class="line">         * 这就相当于B类反过来调用A的方法D </div><div class="line">         */  </div><div class="line">        callBack.solve(result);   </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * 测试类 </div><div class="line"> * @author xiaanming </div><div class="line"> * </div><div class="line"> */  </div><div class="line">public class Test &#123;  </div><div class="line">    public static void main(String[]args)&#123;  </div><div class="line">        /** </div><div class="line">         * new 一个小李 </div><div class="line">         */  </div><div class="line">        Li li = new Li();  </div><div class="line">  </div><div class="line">        /** </div><div class="line">         * new 一个小王 </div><div class="line">         */  </div><div class="line">        Wang wang = new Wang(li);  </div><div class="line">          </div><div class="line">        /** </div><div class="line">         * 小王问小李问题 </div><div class="line">         */  </div><div class="line">        wang.askQuestion(&quot;1 + 1 = ?&quot;);  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="onclick"><a href="#onclick" class="headerlink" title="onclick"></a>onclick</h3><p>上面是一个异步回调，我们看看同步回调吧，onClick（）方法。</p>
<p>现在来分析分析下Android View的点击方法onclick（）;我们知道onclick()是一个回调方法，当用户点击View就执行这个方法，我们用Button来举例。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">//这个是View的一个回调接口  </div><div class="line">/** </div><div class="line"> * Interface definition for a callback to be invoked when a view is clicked. </div><div class="line"> */  </div><div class="line">public interface OnClickListener &#123;  </div><div class="line">    /** </div><div class="line">     * Called when a view has been clicked. </div><div class="line">     * </div><div class="line">     * @param v The view that was clicked. </div><div class="line">     */  </div><div class="line">    void onClick(View v);  </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line">package com.example.demoactivity;  </div><div class="line">  </div><div class="line">import android.app.Activity;  </div><div class="line">import android.os.Bundle;  </div><div class="line">import android.view.View;  </div><div class="line">import android.view.View.OnClickListener;  </div><div class="line">import android.widget.Button;  </div><div class="line">import android.widget.Toast;  </div><div class="line">  </div><div class="line">/** </div><div class="line"> * 这个就相当于Class A </div><div class="line"> * @author xiaanming </div><div class="line"> * 实现了 OnClickListener接口----&gt;背景一 </div><div class="line"> */  </div><div class="line">public class MainActivity extends Activity implements OnClickListener&#123;  </div><div class="line">    /** </div><div class="line">     * Class A 包含Class B的引用-----&gt;背景二 </div><div class="line">     */  </div><div class="line">    private Button button;  </div><div class="line">  </div><div class="line">    @Override  </div><div class="line">    public void onCreate(Bundle savedInstanceState) &#123;  </div><div class="line">        super.onCreate(savedInstanceState);  </div><div class="line">        setContentView(R.layout.activity_main);  </div><div class="line">        button = (Button)findViewById(R.id.button1);  </div><div class="line">          </div><div class="line">        /** </div><div class="line">         * Class A 调用View的方法,而Button extends View-----&gt;A类调用B类的某个方法 C </div><div class="line">         */  </div><div class="line">        button.setOnClickListener(this);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    /** </div><div class="line">     * 用户点击Button时调用的回调函数，你可以做你要做的事 </div><div class="line">     * 这里我做的是用Toast提示OnClick </div><div class="line">     */  </div><div class="line">    @Override  </div><div class="line">    public void onClick(View v) &#123;  </div><div class="line">        Toast.makeText(getApplication(), &quot;OnClick&quot;, Toast.LENGTH_LONG).show();  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>下面是View类的<strong>setOnClickListener</strong>方法，就相当于B类，只把关键代码贴出来。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * 这个View就相当于B类 </div><div class="line"> * @author xiaanming </div><div class="line"> * </div><div class="line"> */  </div><div class="line">public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource &#123;  </div><div class="line">    /** </div><div class="line">     * Listener used to dispatch click events. </div><div class="line">     * This field should be made private, so it is hidden from the SDK. </div><div class="line">     * &#123;@hide&#125; </div><div class="line">     */  </div><div class="line">    protected OnClickListener mOnClickListener;  </div><div class="line">      </div><div class="line">    /** </div><div class="line">     * setOnClickListener()的参数是OnClickListener接口------&gt;背景三 </div><div class="line">     * Register a callback to be invoked when this view is clicked. If this view is not </div><div class="line">     * clickable, it becomes clickable. </div><div class="line">     * </div><div class="line">     * @param l The callback that will run </div><div class="line">     * </div><div class="line">     * @see #setClickable(boolean) </div><div class="line">     */  </div><div class="line">      </div><div class="line">    public void setOnClickListener(OnClickListener l) &#123;  </div><div class="line">        if (!isClickable()) &#123;  </div><div class="line">            setClickable(true);  </div><div class="line">        &#125;  </div><div class="line">        mOnClickListener = l;  </div><div class="line">    &#125;  </div><div class="line">      </div><div class="line">      </div><div class="line">    /** </div><div class="line">     * Call this view&apos;s OnClickListener, if it is defined. </div><div class="line">     * </div><div class="line">     * @return True there was an assigned OnClickListener that was called, false </div><div class="line">     *         otherwise is returned. </div><div class="line">     */  </div><div class="line">    public boolean performClick() &#123;  </div><div class="line">        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);  </div><div class="line">  </div><div class="line">        if (mOnClickListener != null) &#123;  </div><div class="line">            playSoundEffect(SoundEffectConstants.CLICK);  </div><div class="line">              </div><div class="line">            //这个不就是相当于B类调用A类的某个方法D，这个D就是所谓的回调方法咯  </div><div class="line">            mOnClickListener.onClick(this);  </div><div class="line">            return true;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        return false;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>这个例子就是Android典型的回调机制。</p>
<p>线程run()也是一个回调方法，当执行Thread的start（）方法就会回调这个run()方法，还有处理消息都比较经典等等。</p>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><p><a href="http://blog.csdn.net/xiaanming/article/details/17483273" target="_blank" rel="external">http://blog.csdn.net/xiaanming/article/details/17483273</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Callback/" rel="tag"># Callback</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/02/27/201702/TCPIP/" rel="next" title="TCP/IP">
                <i class="fa fa-chevron-left"></i> TCP/IP
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/02/27/201702/编码/" rel="prev" title="Unicode & UTF-8">
                Unicode & UTF-8 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="HoneyGuide" />
          <p class="site-author-name" itemprop="name">HoneyGuide</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">34</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#回调"><span class="nav-number">1.</span> <span class="nav-text">回调</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#举例"><span class="nav-number">2.</span> <span class="nav-text">举例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#onclick"><span class="nav-number">3.</span> <span class="nav-text">onclick</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参考"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HoneyGuide</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  



  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="local-search-pop-overlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


  

</body>
</html>
